package com.ror.java.se.thread;

//同步代码块

public class Demo07 {
    public static void main(String[] args) {
        MyRun mr = new MyRun();
        Thread t1 = new Thread(mr, "窗口1");
        Thread t2 = new Thread(mr, "窗口2");
        Thread t3 = new Thread(mr, "窗口3");
        t1.start();
        t2.start();
        t3.start();
    }

    private static class MyRun implements Runnable {
        static int ticket = 0;
        static final Object lock = MyRun.class; //当前类的字节码对象

        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (lock) {
                    if (ticket >= 100) break;
                    ticket++;
                    System.out.println(Thread.currentThread().getName() + "卖出了第" + ticket + "张票！");
                }
            }
        }
    }
}
